
#ifndef CHORDNODE_H_

#define CHORDNODE_H_

#include "node.h"
#include "packet.h"

class Application;
class ChordMsg;

class ChordNode : public Node{
	public:
		ChordNode( chord_node_id_t c_nid, node_id_t n_id ); 
		virtual ~ChordNode(); 

		virtual void onEvent( Event *event );
		
		//implementation of Node
		virtual void onReceived( Packet *packet );
		virtual void onJoining();
		virtual void onDeparture();

		Application*   app() { return app_; }
		chord_net_id_t getChordNodeID() const { return chord_nid_; }
		void           setStartupNetID( chord_net_id_t id ) { startup_net_id_ = id; }

		void processMessage( ChordMsg *msg );

		bool merge( chord_net_id_t from, chord_net_id_t to );

		ChordProtocol* getProtocolByID( chord_net_id_t id );

		bool isInNetwork( chord_net_id_t net_id );
	protected:
		void searchKey( chord_key_t key, chord_net_id_t net_id );
		void clear();
		
	protected:
		chord_node_id_t chord_nid_;
		Application *app_;
		TChordProtocolList protocols_;
		chord_net_id_t startup_net_id_;

	friend class ChordGod;
};

#endif /* CHORDNODE_H_ */

